<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="股票代码" prop="symbol">
        <el-select v-model="queryParams.symbol" placeholder="请选择股票" filterable style="width: 240px">
          <el-option v-for="(item, index) in stocks" :key="index" :label="item.name" :value="item.symbol"
            :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="交易日期" prop="tradeDate">
        <el-date-picker v-model="queryParams.tradeDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择交易日期"
          clearable style="width: 240px" />
      </el-form-item>
      <el-form-item label="开盘价" prop="open">
        <el-input v-model="queryParams.open" placeholder="请输入开盘价" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="收盘价" prop="close">
        <el-input v-model="queryParams.close" placeholder="请输入收盘价" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="最高价" prop="high">
        <el-input v-model="queryParams.high" placeholder="请输入最高价" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="最低价" prop="low">
        <el-input v-model="queryParams.low" placeholder="请输入最低价" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="成交量" prop="vol">
        <el-input v-model="queryParams.vol" placeholder="请输入成交量" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="成交额" prop="amount">
        <el-input v-model="queryParams.amount" placeholder="请输入成交额" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="振幅" prop="amplitude">
        <el-input v-model="queryParams.amplitude" placeholder="请输入振幅" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="涨跌幅" prop="pctChg">
        <el-input v-model="queryParams.pctChg" placeholder="请输入涨跌幅" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="涨跌额" prop="chg">
        <el-input v-model="queryParams.chg" placeholder="请输入涨跌额" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="换手率" prop="turn">
        <el-input v-model="queryParams.turn" placeholder="请输入换手率" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['stock:daily:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs v-model="activeName" type="card" class="el-tabs" @tab-click="handleTabClick">
      <el-tab-pane label="表" name="tab-table">
        <el-table v-loading="loading" :data="dailyList">
          <el-table-column label="交易日期" align="center" prop="tradeDate" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.tradeDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="股票代码" align="center" prop="symbol" />
          <el-table-column label="开盘价" align="center" prop="open" />
          <el-table-column label="收盘价" align="center" prop="close" />
          <el-table-column label="最高价" align="center" prop="high" />
          <el-table-column label="最低价" align="center" prop="low" />
          <el-table-column label="成交量" align="center" prop="vol" />
          <el-table-column label="成交额" align="center" prop="amount" />
          <el-table-column label="振幅" align="center" prop="amplitude" />
          <el-table-column label="涨跌幅" align="center" prop="pctChg" />
          <el-table-column label="涨跌额" align="center" prop="chg" />
          <el-table-column label="换手率" align="center" prop="turn" />
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-tab-pane>
      <el-tab-pane label="图" name="tab-image">
          <div ref="stockImage" style="width: 1000px;height: 1000px"/>
      </el-tab-pane>
    </el-tabs>
    <!-- 添加或修改股票日线对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dailyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="renderField(true, true)" label="交易日期" prop="tradeDate">
          <el-date-picker clearable v-model="form.tradeDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择交易日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="股票代码" prop="symbol">
          <el-input v-model="form.symbol" placeholder="请输入股票代码" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="开盘价" prop="open">
          <el-input v-model="form.open" placeholder="请输入开盘价" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="收盘价" prop="close">
          <el-input v-model="form.close" placeholder="请输入收盘价" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="最高价" prop="high">
          <el-input v-model="form.high" placeholder="请输入最高价" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="最低价" prop="low">
          <el-input v-model="form.low" placeholder="请输入最低价" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="成交量" prop="vol">
          <el-input v-model="form.vol" placeholder="请输入成交量" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="成交额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入成交额" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="振幅" prop="amplitude">
          <el-input v-model="form.amplitude" placeholder="请输入振幅" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="涨跌幅" prop="pctChg">
          <el-input v-model="form.pctChg" placeholder="请输入涨跌幅" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="涨跌额" prop="chg">
          <el-input v-model="form.chg" placeholder="请输入涨跌额" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="换手率" prop="turn">
          <el-input v-model="form.turn" placeholder="请输入换手率" />
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Daily">
import { listAllBasic } from "@/api/stock/basic";
import { addDaily, delDaily, getDaily, listAllDaily, listDaily, updateDaily } from "@/api/stock/daily";
import { initKlineData, initKlineEcharts } from "@/utils/stock/kline";


const { proxy } = getCurrentInstance();

const stocks = ref([])
const dailyList = ref([]);
const allDailyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const activeName = ref("tab-table");
const stockImage = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tradeDate: null,
    symbol: null,
    open: null,
    close: null,
    high: null,
    low: null,
    vol: null,
    amount: null,
    amplitude: null,
    pctChg: null,
    chg: null,
    turn: null,
  },
  rules: {
    tradeDate: [
      { required: true, message: "交易日期不能为空", trigger: "blur" }
    ],
    symbol: [
      { required: true, message: "股票代码不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询股票列表 */
function getBasicList() {
  listAllBasic().then(response => {
    stocks.value = response.rows;
    queryParams.value.symbol = stocks.value[0].symbol;
    getList();
  });

}

/** 查询股票日线列表 */
function getList() {
  loading.value = true;
  listDaily(queryParams.value).then(response => {
    dailyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询股票日线列表(不分页) */
function getAll() {
  loading.value = true;
  listAllDaily({"symbol": queryParams.value.symbol}).then(response => {
    allDailyList.value = response.data;
    loading.value = false;
    var data = initKlineData(allDailyList.value);
    initKlineEcharts(stockImage.value, data);
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    tradeDate: null,
    symbol: null,
    open: null,
    close: null,
    high: null,
    low: null,
    vol: null,
    amount: null,
    amplitude: null,
    pctChg: null,
    chg: null,
    turn: null,
    createTime: null,
    updateTime: null,
  };
  proxy.resetForm("dailyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** tab切换  */
function handleTabClick(tab, event) {
  if(tab.props.name == 'tab-table') {
    getList();
  }
  if(tab.props.name == 'tab-image') {
    getAll();
  }
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加股票日线";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getDaily(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改股票日线";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dailyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDaily(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDaily(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除股票日线编号为"' + _ids + '"的数据项？').then(function () {
    return delDaily(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('stock/daily/export', {
    ...queryParams.value
  }, `daily_${new Date().getTime()}.xlsx`);
}

/** 是否渲染字段 */
function renderField(insert, edit) {
  return form.value.id == null ? insert : edit;
}
getBasicList();

</script>